package com.becca.cake.core.print;

import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import sun.awt.AppContext;

public class PrintServiceHelp {

    /**
     * 打印头信息
     * 
     * @return
     */
    public static PrintService getPrintHeader(String printServiceName) {
        AppContext.getAppContext().put(PrintServiceLookup.class.getDeclaredClasses()[0], null);
        // 设置打印格式，因为未确定类型，所以选择autosense
        // 查找所有的可用的打印服务
        PrintService printService[] = PrintServiceLookup.lookupPrintServices(DocFlavor.URL.JPEG,
            null);
        return getPrintServiceByName(printService, printServiceName);
    }

    /**
     * 获取对应的打印机对象
     * 
     * @author hejinguo
     * @param printServiceArray
     * @param printServiceName
     * @return
     */
    public static PrintService getPrintServiceByName(PrintService printServiceArray[],
                                                     String printServiceName) {
        PrintService printService = null;
        for (PrintService printSer : printServiceArray) {
            if (printServiceName.equals(printSer.getName())) {
                printService = printSer;
                break;
            }
        }
        return printService;
    }

}
